<?php
/**
 * vanephp web framework
 * 图片助手类
 *
 * @author aray <ofree@qq.com>
 * @link http://www.vanephp.com/s
 * @copyright Copyright &copy; 2010 aray
 * @license http://www.vanephp.com/license
 * @package helper
 */

if ( ! defined ( 'INVANE' ) )
	exit ( '!' );

/**
 * 图片助手类
 * @author aray
 *
 */
class vane_helper_image {
	private $width = 0;
	private $height = 0;
	private $type = null;
	private $img = null;
	public $font = '';
	
	/**
	 * 从文件路径创建图像
	 * @param string $file	文件路径
	 */
	public function createFromFile ( $file ) {
		list ( $this->width, $this->height, $this->type ) = getimagesize ( $file );
		switch ( $this->type ) {
			case IMAGETYPE_JPEG :
				$this->img = imagecreatefromjpeg ( $file );
				break;
			case IMAGETYPE_GIF :
				$this->img = imagecreatefromgif ( $file );
				break;
			case IMAGETYPE_BMP :
				$this->img = imagecreatefromwbmp ( $file );
				break;
			case IMAGETYPE_PNG :
				$this->img = imagecreatefrompng ( $file );
				break;
		}
		return $this;
	}
	
	/**
	 * 生成缩略图(可以单指定高或宽，程序自动等比例缩放)
	 * @param string $savePath	保存路径
	 * @param int $width		宽度
	 * @param int $height		高度
	 * @param boolean $output	是否输出
	 */
	public function makeThumb ( $savePath = null, $width = null, $height = null, $output = true ) {
		
		if ( $width == null && $height != null ) {
			$width = $this->width / $this->height * $height;
		}
		if ( $width != null && $height == null ) {
			$height = $this->height / $this->width * $width;
		}
		
		$imgD = imagecreatetruecolor ( $width, $height );
		imagecopyresampled ( $imgD, $this->img, 0, 0, 0, 0, $width, $height, $this->width, $this->height );
		
		// 输出图像
		$this->img = $imgD;
		if ( $output ) {
			$this->outputImage ( $savePath );
		} else {
			$this->width = $width;
			$this->height = $height;
		}
		
		return $this;
	}
	
	/**
	 * 按百分比生成缩略图
	 * @param string $savePath 	保存路径
	 * @param int $percent		比例
	 */
	public function makeThumbByPercent ( $savePath = null, $percent = .5 ) {
		$this->makeThumb ( $savePath, $this->width * $percent );
		return $this;
	}
	
	/**
	 * 添加文字水印
	 * @param string $savePath	保存路径
	 * @param string $string	水印文字
	 * @param string $fontsize	字体大小
	 * @param string $color		字段颜色,用数组表示如 array(255,0,0)
	 * @param boolean $output	是否输出
	 */
	function makeTextWater ( $savePath = null, $string, $fontsize = null, $color = null, $output = true ) {
		if ( $this->font == null || ! file_exists ( $this->font ) ) {
			trigger_error ( '字体文件不存在', E_USER_ERROR );
		}
		
		$fontsize = $fontsize == null ? 14 : $fontsize;
		$color = $color == null ? array (
			255, 0, 0 
		) : $color;
		$textColor = imagecolorallocate ( $this->img, $color[0], $color[1], $color[2] );
		imagettftext ( $this->img, $fontsize, 0, 10, $this->height - $fontsize / 2, $textColor, $this->font, $string );
		
		// 输出图像
		if ( $output ) {
			$this->outputImage ( $savePath );
		}
		
		return $this;
	}
	
	/**
	 * 输出图像
	 * @param $savePath	保存路径
	 */
	function outputImage ( $savePath = null ) {
		header ( 'Content-type: ' . image_type_to_mime_type ( $this->type ) . "\n\n" );
		imagejpeg ( $this->img, $savePath );
	}
}
	